package com.itcam.servlet_;


import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * Author:Cammy.Wu
 * Date:2024-06-27
 * Description:Servlet生命周期
 */


@WebServlet(name = "ServletDemo02LifeCycle")
public class ServletDemo02LifeCycle extends HttpServlet {
    /**
     * 完成servlet对象的初始化
     * 在service接收到第一次请求的时候创建对象
     * 生命周期：从第一次接收请求开始到服务器关闭之后销毁
     * 当在web.xml文件中配置了 <load-on-startup>1</load-on-startup>,在开启tomcat的时候就会创建servlet对象，中间的数值表示优先级
     * @throws ServletException
     */
    @Override
    public void init() throws ServletException {
        System.out.println("init");
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("ServletDemo02LifeCycle");
        System.out.println("learn servlet lifecycle");
    }

    @Override
    public void destroy() {
        System.out.println("destroy");
    }
}
